/*
 * Copyright (C) 2008 Shane Isbell
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.slideme.atom.parser;

import org.kxml2.io.KXmlParser;
import org.slideme.atom.parser.entity.AtomEntityContext;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;

public class Category {

    private String term;

    private String label;

    private String scheme;

    public String getTerm() {
        return term;
    }

    public void setTerm(String term) {
        this.term = term;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getScheme() {
        return scheme;
    }

    public void setScheme(String scheme) {
        this.scheme = scheme;
    }

    public static Category marshalFrom(KXmlParser parser, AtomEntityContext ctx) throws IOException, XmlPullParserException {
        if (parser == null) {
            throw new IllegalArgumentException("parser");
        }
        Category category = new Category();
        int attributeCount = parser.getAttributeCount();
        for (int i = 0; i < attributeCount; i++) {
            String name = parser.getAttributeName(i);
            if (name.equals("term")) {
                category.setTerm(parser.getAttributeValue(i));
            } else if (name.equals("scheme")) {
                category.setScheme(parser.getAttributeValue(i));
            } else if (name.equals("label")) {
                category.setLabel(parser.getAttributeValue(i));
            }
        }
        if(category.getTerm() == null) {
            throw new IOException("Missing required term attribute");            
        }
        parser.nextTag();
        return category;
    }
}
